package com.resou.reader.view.booklebel.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.view.booklebel.LabelBean;
import com.resou.reader.view.booklebel.LabelNameBean;

import java.util.List;

/**
 * 推荐部分适配器
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> {

    Context context;
    List<BookLabelBean.FilterDetailBean> list; //源数据

    public RecommendAdapter(Context context, List<BookLabelBean.FilterDetailBean> list) {
        this.context = context;
        this.list = list;
    }

    public IRecommendListener iRecommendListener;

    public void setiRecommendListener(IRecommendListener recommendListener) {
        this.iRecommendListener = recommendListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_item_label, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.checkedTextView.setChecked(list.get(i).isCheck());
        viewHolder.checkedTextView.setText(list.get(i).getTypeDesc());
        viewHolder.checkedTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 将数据全部置为未选中状态
                 */
                for (BookLabelBean.FilterDetailBean labelBean : list) {
                    labelBean.setCheck(false);
                }
                /**
                 * 然后将当前数据 状态改为选中状态
                 */
                list.get(i).setCheck(true);
                /**
                 * 刷新数据
                 */
                notifyDataSetChanged();
                if (null != iRecommendListener) {
                    iRecommendListener.setIRecommendListener(list.get(i));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * 更新数据
     *
     * @param list
     */
    public void updata(List<BookLabelBean.FilterDetailBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (null != this.list) {
            this.list.clear();
            this.notifyDataSetChanged();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CheckedTextView checkedTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            checkedTextView = itemView.findViewById(R.id.cbLabel);
        }
    }

    public interface IRecommendListener {
        void setIRecommendListener(BookLabelBean.FilterDetailBean bean);
    }
}
